#include "ticketdevfile.h"
ticketdevfile::ticketdevfile()
{
    this->fileName.sprintf("ticket.ini");
    if(QFile::exists(this->fileName))
    {
        this->read();
    }
    else
    {
        this->reast();
        this->write();
    }
}
//读取设置
int ticketdevfile::read()
{
    QFile myfile(fileName);
    QByteArray buffer;
    QString str;
    QStringList strList;
    QMap<QString,QString> map;
    //read ticdet.ini
    myfile.open(QIODevice::ReadOnly);
    while(1)
    {
        buffer = myfile.readLine();
        if(buffer.length() == 0)
        {
            break;
        }
        str = QString(buffer);
        strList = str.split("=");
        if(strList.count() >=2)
        {
            map.insert(strList[0],strList[1]);
        }
    }
    myfile.close();
    //加载设置
    comDev[0].com_type  = map["com1_dev"].toInt();
    comDev[0].com_model = map["com1_model"].toInt();

    comDev[1].com_type  = map["com2_dev"].toInt();
    comDev[1].com_model = map["com2_model"].toInt();

    comDev[2].com_type  = map["com3_dev"].toInt();
    comDev[2].com_model = map["com3_model"].toInt();

    comDev[3].com_type  = map["com4_dev"].toInt();
    comDev[3].com_model = map["com4_model"].toInt();

    for(int i=0;i<4;i++)
    {
        qDebug() << "com" << i+1<< "_type=" << comDev[i].com_type;
        qDebug() << "com" << i+1<< "_model=" << comDev[i].com_model;
    }
    return -1;
}
//写入设置
int ticketdevfile::write()
{
    QString writeBuf;
    QFile myfile(fileName);
    myfile.open(QIODevice::WriteOnly);
    for(int i=0;i<4;i++)
    {
        writeBuf.sprintf("com%d_dev=%d\r\n",i+1,comDev[i].com_type);
        myfile.write(writeBuf.toAscii());
        writeBuf.sprintf("com%d_model=%d\r\n",i+1,comDev[i].com_model);
        myfile.write(writeBuf.toAscii());
        myfile.write("\r\n");
    }
    myfile.close();
    return -1;
}
//复位设置
int ticketdevfile::reast()
{
    comDev[0].com_type = 1;
    comDev[0].com_model = 1;

    comDev[1].com_type = 1;
    comDev[1].com_model = 1;

    comDev[2].com_type = 0;
    comDev[2].com_model = 0;

    comDev[3].com_type = 4;
    comDev[3].com_model = 1;
    return -1;
}
